package com.abucloud.logbackdemo.controller;

import com.abucloud.logbackdemo.dto.LoginUserInfoDTO;
import com.abucloud.logbackdemo.exception.BusinessException;
import com.abucloud.logbackdemo.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("login")
    public String userLogin(HttpServletRequest request) throws IOException {
        return this.userInfoService.userLogin();
    }

    @PostMapping("testEx")
    public LoginUserInfoDTO testEx(@RequestBody LoginUserInfoDTO loginUserInfoDTO) {
        throw new BusinessException(500, "testEx异常");
    }

    @PostMapping("user")
    public LoginUserInfoDTO queryUserInfo(
            @Validated @RequestParam("loginAccount") String loginAccount,
            @Validated @RequestParam("password") String password) {

        log.info("请求参数：{},{}", loginAccount, password);
        LoginUserInfoDTO loginUserInfoDTO = new LoginUserInfoDTO();
        loginUserInfoDTO.setLoginAccount(loginAccount);
        loginUserInfoDTO.setPassword(password);
        return loginUserInfoDTO;
    }

    @PostMapping("user02")
    public LoginUserInfoDTO user02(@RequestBody LoginUserInfoDTO loginUserInfoDTO) {
        return loginUserInfoDTO;
    }

    @PostMapping("user04/{loginAccount}")
    public LoginUserInfoDTO user03(@PathVariable("loginAccount") String loginAccount) {
        LoginUserInfoDTO loginUserInfoDTO = new LoginUserInfoDTO();
        loginUserInfoDTO.setLoginAccount(loginAccount);
        return loginUserInfoDTO;
    }

    @GetMapping("user05")
    public LoginUserInfoDTO user05(@Validated @RequestParam("loginAccount") String loginAccount,
                                   @Validated @RequestParam("password") String password) {
        LoginUserInfoDTO loginUserInfoDTO = new LoginUserInfoDTO();
        loginUserInfoDTO.setLoginAccount(loginAccount);
        loginUserInfoDTO.setPassword(password);
        return loginUserInfoDTO;
    }

    @PostMapping("user06")
    public void user02(HttpServletRequest request) {
        log.info("user06");
    }

    @GetMapping("user07")
    public LoginUserInfoDTO user07(LoginUserInfoDTO loginUserInfoDTO) {
        return loginUserInfoDTO;
    }

    @PostMapping("user03")
    public void user02(@RequestParam("name") String name) {
        int a = 1 / 0;
        log.info(name);
    }

    @PostMapping("user04")
    public LoginUserInfoDTO user05() {
        // throw new RuntimeException();
        return new LoginUserInfoDTO().setLoginAccount("123");
    }

}